/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package negocio;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import persistencia.dao.hibernate.TicketDAOImpl;
import persistencia.pojo.Ticket;
import persistencia.util.HibernateUtil;

/**
 *
 * @author Victor
 */
public class TicketManager {
    private TicketDAOImpl mgr;
    private Session session;

    public TicketManager() {
        mgr = new TicketDAOImpl();
        session = HibernateUtil.getSessionFactory().openSession();
    }
    public void closeSesion(){
        session.close();
    }
    public void saveTicket(Ticket ticket){
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            mgr.saveTicket(session, ticket);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        } finally{
            //session.close();
        }
    }
    public void deleteTicket(Ticket ticket){
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            mgr.deleteTicket(session, ticket);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        } finally{
            //session.close();
        }
    }
    public void updateTicket(Ticket ticket){
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            mgr.updateTicket(session, ticket);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        } finally{
            //session.close();
        }
    }

    public List<Ticket> getAllTickets(){
        Transaction tx = null;
        List<Ticket> retorno = null;
        try {
            tx = session.beginTransaction();
            retorno = mgr.getAllTickets(session);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        } finally{
            //session.close();
        }
        return retorno;
    }
    public List<Ticket> getTicketsWhere(String strColTabla, String strValueCol, String strPagActual){
        Transaction tx = null;
        List<Ticket> retorno = null;
        try {
            tx = session.beginTransaction();
            retorno = mgr.getTicketsWhere(session, strColTabla, strValueCol, strPagActual);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        } finally{
            //session.close();
        }
        return retorno;
    }
    public List<Ticket> getTicketsAsignadoSoporte(String co_soporte){
        Transaction tx = null;
        List<Ticket> retorno = null;
        try {
            tx = session.beginTransaction();
            retorno = mgr.getTicketsAsignadoSoporte(session, co_soporte);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        } finally{
            //session.close();
        }
        return retorno;
    }
    public Ticket getTicket(String codticket){
        Transaction tx = null;
        Ticket retorno = null;
        try {
            tx = session.beginTransaction();
            retorno = mgr.getTicket(session,codticket);
            tx.commit();
        } catch (Exception e) {
            tx.commit();
        } finally{
            //session.close();
        }
        return retorno;
    }
    
    public String idTicket() throws Exception {
        Transaction tx = null;
        String retorno = null;
        try {
            tx = session.beginTransaction();
            retorno= mgr.idTicket(session);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        } finally{
            //session.close();
        }
        return retorno;
    }
}
